package com.dragon.client.controller.customer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.client.config.annotation.Login;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.customer.CustomerService;
import com.dragon.repository.domain.dto.ContractDTO;
import com.dragon.repository.domain.dto.CustomerDTO;
import com.dragon.repository.domain.vo.CustomerVO;
import com.dragon.repository.entity.Contract;
import com.dragon.repository.entity.Customer;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客户管理接口
 *
 * @author Charlotte
 */

@RestController
@Api(tags = "客户")
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    CustomerService service;

    @GetMapping("list")
    @ApiOperation(value = "客户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "search", value = " 搜索条件，可以为空", required = false, type = "string"),
            @ApiImplicitParam(name = "page", value = "页数", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, type = "int"),
            @ApiImplicitParam(name = "status", value = " 审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "applicationId", value = "用户id", required = false, type = "int"),
            @ApiImplicitParam(name = "settlement", value = "结算方式", required = false, type = "int")
    })
    public ResponseData<List<CustomerDTO>> selectCustomerList(
            @RequestParam(required = false) String search,
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer applicationId,
            @RequestParam(required = false) Integer settlement
    ) {
        Page<Customer> list = service.selectCustomerList(search, status, page, limit, applicationId, settlement);
        return ResponseData.success(CustomerDTO.convertList(list.getRecords()), list.getTotal());
    }

    @PostMapping("add")
    @ApiOperation(value = "新增客户")
    @Log
    @AddPage
    public ResponseData addCustomer(@RequestBody @Validated(Insert.class) CustomerVO vo) {
        return ResponseData.success(service.addCustomer(CustomerVO.convert(vo)));
    }

    @PostMapping("update")
    @ApiOperation(value = "修改客户")
    @Log
    public ResponseData<Integer> updateCustomer(@RequestBody @Validated(Update.class) CustomerVO vo) {
        service.updateCustomer(CustomerVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除客户")
    @Log
    public ResponseData<Integer> deleteCustomer(@ApiParam(name = "id", type = "int", required = true, value = "客户id") @RequestParam Integer id) {
        service.deleteCustomerId(id);
        return ResponseData.success();
    }

    @PostMapping("transmit")
    @ApiOperation(value = "转接客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户id", required = true, type = "int"),
            @ApiImplicitParam(name = "adminId", value = "需要转接的用户id", required = true, type = "int")
    })
    public ResponseData transmit(@RequestParam Integer id, @RequestParam Integer adminId) {
        service.changeAdminId(id, adminId);
        return ResponseData.success();
    }

    @GetMapping("listId")
    @ApiOperation("根据id查看客户详情")
    public ResponseData listCustomerId(@RequestParam
                                       @ApiParam(name = "id", value = "客户id", required = true, type = "int") Integer id) {
        return ResponseData.success(service.selectDetailById(id));
    }


    @GetMapping("listById")
    @ApiOperation("根据客户id查看合同详细列表")
    public ResponseData<List<ContractDTO>> list(@ApiParam(name = "customerId", value = "客户id", required = true, type = "int") @RequestParam Integer customerId) {
        Page<Contract> page = service.listId(customerId);
        return ResponseData.success(ContractDTO.convertList(page.getRecords()));
    }

    @PostMapping("newCustomerNum")
    @ApiOperation("当月新增客户信息")
    public ResponseData<Map<String, Object>> newCustomerNum() {
        Map<String, Object> map = service.newCustomerNub();
        return ResponseData.success(map);
    }

    @PostMapping("distinct")
    @ApiOperation("检测客户是否添加")
    public ResponseData distinctByCustomerName(
            @ApiParam(name = "name", required = true, type = "String", value = "客户名称") @RequestParam String name) {
        service.distinctCustomerName(name);
        return ResponseData.success();
    }

    @PostMapping("allocation")
    @ApiOperation("分配客户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, type = "int", value = "主表id"),
            @ApiImplicitParam(name = "adminId", required = true, type = "int", value = "用户id")
    })
    public ResponseData updateAdminId(
            @RequestParam Integer id,
            @RequestParam Integer adminId) {
        service.updateAdminId(id,adminId);
        return ResponseData.success();
    }

    @PostMapping("viewListByAdminId")
    @ApiOperation(value = "获取当前用户下的客户")
    public ResponseData<List<CustomerDTO>> listByAdminId(
            @ApiParam(hidden = true) @Login Integer adminId,
            @ApiParam(name = "search", value = "搜索参数", type = "string", required = false) @RequestParam(required = false) String search){
        List<Customer> list = service.listByAdminId(adminId,search);
        return ResponseData.success(CustomerDTO.convertList(list), (long) list.size());
    }

    @GetMapping("informationOfCustomer")
    @ApiOperation(value = "合同总信息之客户")
    public ResponseData<CustomerDTO> informationOfCustomer(
            @ApiParam(name = "contractId", value = "合同id", required = true, type = "int")
            @RequestParam Integer contractId){
        Customer customer = service.selectDetailByContractId(contractId);
        return ResponseData.success(CustomerDTO.convert(customer));
    }


    @PostMapping("upgrade")
    @ApiOperation(value = "升级成月结客户")
    public ResponseData upgradeSettlement(
            @ApiParam(name = "id", value = "客户id", required = true, type = "int")
            @RequestParam Integer id
    ){
        service.upgradeSettlement(id);
        return ResponseData.success();
    }


    @GetMapping("customerNumberByYearMonth")
    @ApiOperation(value = "客户成交量按年月查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",required = false,type = "string",value = "客户名称"),
            @ApiImplicitParam(name = "startTime",required = false,type = "string",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",required = false,type = "string",value = "结束时间"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<CustomerDTO>> selectCountCustomerByYearMonth(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Customer> customerPage = service.selectCountCustomerByYearMonth(name, startTime, endTime, page, limit);

        return ResponseData.success(CustomerDTO.convertList(customerPage.getRecords()),customerPage.getTotal());
    }


    @GetMapping("customerNumber")
    @ApiOperation(value = "客户成交量查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",required = false,type = "string",value = "客户名称"),
            @ApiImplicitParam(name = "startTime",required = false,type = "string",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",required = false,type = "string",value = "结束时间"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<CustomerDTO>> selectCountContractBy(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam Integer page,
            @RequestParam Integer limit){
        Page<Customer> customerPage = service.selectCountContractBy(name, startTime, endTime, page, limit);

        return ResponseData.success(CustomerDTO.convertList(customerPage.getRecords()),customerPage.getTotal());
    }

}
